package com.lanou.moving.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.EveryoneMore;
import com.lanou.moving.utils.Exchange;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import java.util.List;

/**
 * Created by 刘琪 on 15/10/28.
 */
public class EveryoneMoreAdapter extends BaseAdapter {

    private static Context context;
    private List<EveryoneMore.DataEntity.SongsEntity> songsEntities;
    private OnMenuClickListener onMenuClickListener;
    private String url;

    final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");

    public EveryoneMoreAdapter(Context context, List<EveryoneMore.DataEntity.SongsEntity> songsEntities) {
        this.context = context;
        this.songsEntities = songsEntities;
    }

    public interface OnMenuClickListener {
        void onMenuClick(EveryoneMore.DataEntity.SongsEntity songsEntity);
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    @Override
    public int getCount() {
        return songsEntities != null && songsEntities.size() > 0 ? songsEntities.size() : 0;
    }

    @Override
    public EveryoneMore.DataEntity.SongsEntity getItem(int position) {
        return songsEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.songs_list_listview_item, null);
            holder = new ViewHolder();
            holder.songs_list_position = (TextView) convertView.findViewById(R.id.songs_list_position);
            holder.songs_list_songName = (TextView) convertView.findViewById(R.id.songs_list_songName);
            holder.songs_list_item_singer = (TextView) convertView.findViewById(R.id.songs_list_item_singer);
            holder.songs_list_item_number = (TextView) convertView.findViewById(R.id.songs_list_item_number);
            holder.list_more = (ImageView) convertView.findViewById(R.id.list_more);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final EveryoneMore.DataEntity.SongsEntity songsEntity = getItem(position);
        url = songsEntity.getUrlList().get(0).getUrl();
        if (songsEntity != null) {
            String num = Exchange.getCount(songsEntity.getFavorites());

            holder.songs_list_position.setText(position + 1 + "");
            holder.songs_list_songName.setText(songsEntity.getName());
            holder.songs_list_item_singer.setText(songsEntity.getSingerName());
            holder.songs_list_item_number.setText(num);
            holder.list_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMenuClickListener != null) {
                        onMenuClickListener.onMenuClick(songsEntities.get(position));
                    }
                }

            });

        }
        return convertView;
    }

    private void initShare() {

        // 添加QQ平台
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler((Activity) context, "100424468",
                "c7394704798a158208a74ab60104f0ba");
        qqSsoHandler.addToSocialSDK();
        // 配置新浪微博SSO
        mController.getConfig().setSsoHandler(new SinaSsoHandler());

        // 添加qq空间平台
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler((Activity) context, "100424468",
                "c7394704798a158208a74ab60104f0ba");
        qZoneSsoHandler.addToSocialSDK();
    }

    private void QQShare() {
        QQShareContent qqShareContent = new QQShareContent();
        //设置分享文字
        qqShareContent.setShareContent("第一次分享东西， 好好看");
        //设置分享title
        qqShareContent.setTitle("哈哈哈");
        //设置点击分享内容的跳转连接
        qqShareContent.setTargetUrl(url);
        //????
        mController.setShareMedia(qqShareContent);
        QZoneShareContent qzone = new QZoneShareContent();
        //设置分享文字
        qzone.setShareContent("第一次给你分享东西，好好看");
        //设置分享内容的标题
        qzone.setTitle("哈哈哈");
        //设置点击消息的跳转URL
        qzone.setTargetUrl(url);
        mController.setShareMedia(qzone);
    }

    private class ViewHolder {
        private ImageView list_more;
        private TextView songs_list_position;
        private TextView songs_list_songName;
        private TextView songs_list_item_singer;
        private TextView songs_list_item_number;
    }
}
